$(function() {

	/*为菜单中的 加入购物车控制钮绑定事件*/
	$("button.shop-cartbutton").on('click', function() {
		var foodInfo = $(this).parent().parent().parent().parent();
		addFood(foodInfo);
		$(this).addClass('hide');
		$(this).next().removeClass('hide');
	});
	
	/*为菜单中的 +控制钮绑定事件*/
	$("button.ctrl.plus").on('click', function() {
		var count = $(this).prev();
		foodPlusById(count.attr('data-food-id'));
	});
	
	/*为菜单中的 -控制钮绑定事件*/
	$("button.ctrl.minus").on('click', function() {
		var count = $(this).next();
		foodMinusById(count.attr('data-food-id'));
	});
	/*为购物车中的 提交控制钮绑定事件*/
	$("button.shop-cartfooter-checkout").on('click', function() {
		var flag = $(this).attr('data-order-first');
		getShopInfo(flag);
	});
	
	/*菜系选择*/
	$("#foodtypes").on('change', function(){ 
		//alert($(this).val());
		var id = $(this).val();
		if (id == 0) {
			$(".shopmenu-food").removeClass('hide');
		}else if (id > 0) {
			$(".shopmenu-food").addClass('hide');
			$(".shopmenu-food[data-food-type='"+id+"']").removeClass('hide');
		}
	});
	
});

/*获取购物车中的菜品信息*/
function getShopInfo(flag) {
	var countArr = new Array();
	var idArr = new Array();
	var totalPrice = $("p.price").text();
	var foods = $("#shopbasket").find("div.shop-cartbasket-tablerow");
	for (var i = 0; i < foods.length; i++) {
		var c = $(foods[i]).find("input").val();
		countArr.push(c);
		var id = $(foods[i]).attr("data-food-id");
		idArr.push(id);
	}
//	alert(countArr.length+"  "+idArr.length+"  "+foods.length);
	if (countArr.length == idArr.length && idArr.length == foods.length) {
		var url = "newOrder";
		$.ajax({
			type:'POST',
			url:url,
			traditional: true,
			dataType:'json',
			data:{
				idArr:idArr,
				countArr:countArr,
				totalPrice:totalPrice
			},
			success: function(data) {
				alert('订餐成功！');
				window.location.href="/index";
			},
			error: function(data) {
				alert('link fail');
			}
		});
	}
}

/* 将菜加入购物车 */
function addFood(e) {
	var foodId = $(e).find(".col-4 .shop-cartctrl .ctrl.quantity").attr('data-food-id');
	var foodName = $(e).find(".shopmenu-food-main h3.shopmenu-food-name").text();
	var foodPrice = $(e).find(".shopmenu-food-price").text();
	var foodCount = $(e).find(".ctrl.quantity").val();
	
	/* create element */
	var tablerow = "";
	
	tablerow += "<div class='shop-cartbasket-tablerow' data-food-id='"+foodId+"' >";
	tablerow +=     "<div class='cell itemname ng-binding shop-cartbasket-tablerow-name' title='"+foodName+"'>"+foodName+"</div>";
	tablerow +=     "<div class='cell itemquantity'>";
	tablerow +=         "<button onClick='foodMinusById("+foodId+")'>-</button>";
	tablerow +=         "<input class='ng-valid ng-dirty shop-cartbasket-tablerow-count' data-food-id='"+foodId+"' value='1' >";
	tablerow +=         "<button onClick='foodPlusById("+foodId+")'  >+</button>";
	tablerow +=     "</div>";
	tablerow +=     "<div class='cell itemtotal ng-binding shop-cartbasket-tablerow-food-price' data-food-price='"+foodPrice+"' >¥"+foodPrice+"</div>";
	tablerow += "</div>";
	
	/* append element */
	$("#shopbasket").append(tablerow);
	valuationAll();
	upShopCart();
}

/*将菜从购物车移除*/
function delFood(e) {
	var shopFood = $("#shopbasket").find("div.shop-cartbasket-tablerow[data-food-id='"+e+"']");
	shopFood.remove();
	downShopCart();
}

/*通过ID控制购物车中的菜和菜单中的菜数量 +1*/
function foodPlusById(id) {
	//alert($(this).html());
	var menuCount = $("#menu").find("input[data-food-id='"+id+"']");
	var shopFood = $("#shopbasket").find("div[data-food-id='"+id+"']");
	var shopCount = shopFood.find("input[data-food-id='"+id+"']");
	var value = menuCount.val();
	++value;
	menuCount.val(value);
	shopCount.val(value);
	valuationFood(id);
	valuationAll();
}

/*通过ID控制购物车中的菜和菜单中的菜数量 -1*/
function foodMinusById(id) {
	var menuCount = $("#menu").find("input[data-food-id='"+id+"']");
	var shopFood = $("#shopbasket").find("div[data-food-id='"+id+"']");
	var shopCount = shopFood.find("input[data-food-id='"+id+"']");
	var value = menuCount.val();
	if((value-1) == 0){
		delFood(id);
		menuCount.parent().addClass('hide');
		menuCount.parent().prev().removeClass('hide');
		menuCount.val(1);
	}
	if (value > 1) {
		--value;
	}
	menuCount.val(value);
	shopCount.val(value);
	valuationFood(id);
	valuationAll();
}

/*购物车为空时的设置*/
function shopCartEmpty() {
	var totalPriceNode = $(".shop-cart>.shop-cartfooter>p");
	/*shop-cartfooter-checkout*/
	$('button.shop-cartfooter-checkout').addClass('disabled');
	$('button.shop-cartfooter-checkout').attr('disabled',true);
	$('button.shop-cartfooter-checkout').text('购物车是空的');
	totalPriceNode.addClass('hide');
}

/*购物车不为空时的设置*/
function shopCartNotEmpty() {
	var totalPriceNode = $(".shop-cart>.shop-cartfooter>p");
	/*shop-cartfooter-checkout*/
	$('button.shop-cartfooter-checkout').removeClass('disabled');
	$('button.shop-cartfooter-checkout').attr('disabled',false);
	$('button.shop-cartfooter-checkout').text('去结算 >');
	totalPriceNode.removeClass('hide');
}

/*将购物车上升一行*/
function upShopCart() {
	//alert("openShopCart");
	var shopbasket = $("div#shopbasket");
	var text = shopbasket.css('top');
	
	/*height = 44 * n*/
	var height = getTopHeight(text);
	height = (44+height);
	var t = '-'+height+'px';
	shopbasket.css('top',t);
}

/*将购物车下降一行*/
function downShopCart() {
	//alert("closeShopCart");
	var shopbasket = $("div#shopbasket");
	var text = shopbasket.css('top');
	if (text != '-44px') {

		/*height = 44 * n*/
		var height = getTopHeight(text);
		height = (height-44);
		var t = '-'+height+'px';
		shopbasket.css('top',t);
	}
}

/*计算并更新一道菜的总价*/
function valuationFood(id) {
	var shopFood = $("#shopbasket").find("div[data-food-id='"+id+"']");
	var shopCount = shopFood.find("input[data-food-id='"+id+"']");
	var foodPrice = shopFood.find(".shop-cartbasket-tablerow-food-price");
	var singlePrice = foodPrice.attr("data-food-price");
	var priceText = "¥";
	priceText += (singlePrice*shopCount.val());
	foodPrice.text(priceText);
}

/*更新购物车中所有菜的总价*/
function valuationAll() {
	var totalPriceNode = $(".shop-cart>.shop-cartfooter>p");
	totalPriceNode.text(getTotalPrice());
	
	if (getTotalPrice() > 0) {
		shopCartNotEmpty();
	}else{
		shopCartEmpty();
	}
}



/*计算购物车中所有菜的总价*/
function getTotalPrice() {
	var foods = $("#shopbasket").find("div.shop-cartbasket-tablerow");
	var totalPrice = 0;
	for (var i = 0; i < foods.length; i++) {
		var c = $(foods[i]).find("input").val();
		var p = $(foods[i]).find(".shop-cartbasket-tablerow-food-price").attr("data-food-price");
		totalPrice += (c*p);
	}
	return totalPrice;
}

/*计算购物车中购物篮升起的高度*/
function getTopHeight(text) {
	var t = text.match(/\d+/);
	return (-1+(++t));
}

/*获取元素指定属性的值*/
function getDataValue(e,name) {
	var text = e.attr(name);
	return e.attr(name);
}


/* element model */

/*
<div class="shopmenu-food ng-isolate-scope" id="1267641039">
	<!-- ngIf: food.image_path -->
	<span class="col-1 ng-scope"><a href="javascript:"><img alt="黑椒牛肉丰盛套餐的图片" src="testmenu.webp"></a></span>
	<!-- end ngIf: food.image_path -->
	<div class="col-2 shopmenu-food-main">
		<h3 class="shopmenu-food-name ui-ellipsis ng-binding">黑椒牛肉丰盛套餐</h3>
		<p class="color-mute ui-ellipsis ng-binding" tooltip="通份－黑椒牛肉+蒸蛋+小菜+米饭+紫菜蛋花汤">通份－黑椒牛肉+蒸蛋+小菜+米饭+紫菜蛋花汤</p>
		<p></p>
		<div class="starrating icon-star ng-isolate-scope" title="评分4.1分">
			<span class="icon-star" ng-style="{ width: (rating * 20) + '%' }" style="width: 82.4%;"></span>
		</div>
		<span class="color-mute ng-binding"></span> <span class="color-mute ng-binding"></span>
		<p></p>
	</div>
	<span class="col-3 shopmenu-food-price color-stress ng-binding">37<small class="ng-binding"></small> <!-- ngIf: food.min_purchase && food.min_purchase > 1 --></span>
	<span class="col-4"><div class="ng-isolate-scope">
			<!-- ngIf: !menuFood.hasSpec -->
			<div class="ng-scope">
				<!-- ngIf: !cartItem.quantity && menuFood.stock -->
				<button class="shop-cartbutton ng-binding ng-scope">加入购物车</button>

				<div class="shop-cartctrl ng-scope hide"><button class="ctrl minus">-</button><input class="ctrl quantity ng-pristine ng-valid" min="1" value="1" data-food-id="123"><button class="ctrl plus">+</button></div>
				<!-- end ngIf: !cartItem.quantity && menuFood.stock -->
				<!-- ngIf: !menuFood.stock -->
				<!-- ngIf: cartItem.quantity > 0 || cartItem.quantity === '' -->
			</div>
			<!-- end ngIf: !menuFood.hasSpec -->
			<!-- ngIf: menuFood.hasSpec -->
		</div></span>
</div>
*/

/*
<c:forEach items="${foods}" var="item">
	<div class="shopmenu-food ng-isolate-scope" id="${item.id}">
		<!-- ngIf: food.image_path -->
		<span class="col-1 ng-scope"><a href="javascript:"><img
				alt="${item.foodName}" src="${pageContext.request.contextPath}/static/menuImages/test01.webp"></a></span>
		<!-- end ngIf: food.image_path -->
		<div class="col-2 shopmenu-food-main">
			<h3 class="shopmenu-food-name ui-ellipsis ng-binding">${item.foodName}</h3>
			<p class="color-mute ui-ellipsis ng-binding"
				tooltip="${item.remark}">${item.remark}</p>
			<p></p>
			
			<span class="color-mute ng-binding"></span> <span
				class="color-mute ng-binding"></span>
			<p></p>
		</div>
		<span class="col-3 shopmenu-food-price color-stress ng-binding">${item.price}<small
			class="ng-binding"></small> <!-- ngIf: food.min_purchase && food.min_purchase > 1 --></span>
		<span class="col-4"><div class="ng-isolate-scope">
				<!-- ngIf: !menuFood.hasSpec -->
				<div class="ng-scope">
					<!-- ngIf: !cartItem.quantity && menuFood.stock -->
					<button class="shop-cartbutton ng-binding ng-scope">加入购物车</button>

					<div class="shop-cartctrl ng-scope hide">
						<button class="ctrl minus">-</button>
						<input class="ctrl quantity ng-pristine ng-valid" min="1"
							value="1" data-food-id="${item.id}">
						<button class="ctrl plus">+</button>
					</div>
					<!-- end ngIf: !cartItem.quantity && menuFood.stock -->
					<!-- ngIf: !menuFood.stock -->
					<!-- ngIf: cartItem.quantity > 0 || cartItem.quantity === '' -->
				</div>
				<!-- end ngIf: !menuFood.hasSpec -->
				<!-- ngIf: menuFood.hasSpec -->
			</div></span>
	</div>
</c:forEach>
*/

/* test */
function test() {
	alert();
}